<?php

$system_folder = "system";
$application_folder = "application";

define('BASEPATH', dirname (__FILE__)."/../../../../../");
define('APPPATH', BASEPATH.$system_folder."/".$application_folder.'/');
require_once(BASEPATH.$system_folder."/helpers/file_helper.php");

// Pull in the NuSOAP code
require_once(BASEPATH.$system_folder.'/libraries/nusoap/nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('km_log_ws', 'http://lokal.si_del_2.del.ac.id/system/application/modules/lkm/ws/lkm_log_ws.php');

$server->wsdl->schemaTargetNamespace="http://lokal.si_del_2.del.ac.id/system/application/modules/lkm/ws/lkm_log_ws.php?wsdl";


// Register the method to expose
$server->register('insertLog',                // method name
    array('nim' => 'xsd:string','dt_log' => 'xsd:string','status' => 'xsd:string'),        // input parameters
    array('return' => 'xsd:string'),      // output parameters
    'http://lokal.si_del_2.del.ac.id/system/application/modules/lkm/ws/lkm_log_ws.php',                      // namespace
    'false',                // soapaction
    'rpc',                                // style
    'literal',                            // use
    'Inser log to db'            // documentation
);
// Define the method as a PHP function
function insertLog($nim, $dt_log, $status) {
	//prepare data
	//echo "$nim";
	$link = db_connect();

	$query = "SELECT * FROM `aka_m_student` WHERE `key` = '$nim' ";

	$result = mysql_query($query,$link);

			
	if(mysql_num_rows($result) != 0 ) {
		
		$query = "SELECT * FROM `lkm_t_log_km` where `key_aka_student`='$nim' and `dt_log`='$dt_log' and `key_lkm_status`='$status'";
	
		$result = mysql_query($query, $link);
		
		if(mysql_num_rows($result) == 0 ) {

			$query = "INSERT INTO `lkm_t_log_km` (`key_aka_student`, `dt_log`, `key_lkm_status`) 
					VALUES ('$nim', '$dt_log', '$status') ";
		
			if( mysql_query($query, $link) ) {
				return 1;
			}
			
		}else{
			//data sudah ada di database
			return -1;
		}
		
	}else{
		//nim tidak ada di master mahasiswa
		//return 0;
	}
	
     //return "Hello, $nim $dt_log $status";
}


// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);

function db_connect() {
	$link = mysql_connect('localhost', 'root', 'mysql');
	if (!$link) {
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("si_del",$link);
	//echo 'Connected successfully';
	return $link;
}

?>
